///
/// $Id: storage.cpp,v 1.4 2005/10/16 08:35:49 olegus Exp $
///

#include "cmain.h"
#include "storage.h"

////////////////////////////////////////////////////////////////////////////

FileOnDisk_c::FileOnDisk_c( const char* szName, bool isRead, bool isBinary )
{
    strcpy( m_szName, szName );
    m_bModeRead = isRead;
    if( m_bModeRead )
    {
        if( *m_szName ) m_tFile = fopen( m_szName, isBinary ? "rb" : "rt" );
        else            m_tFile = stdin;
    }
    else
    {
        if( *m_szName ) m_tFile = fopen( m_szName, isBinary ? "wb" : "wt" );
        else            m_tFile = stdout;
    }
    Assert( m_tFile && "file opening failed" );
}

FileOnDisk_c::~FileOnDisk_c()
{
    Flush();
    if( *m_szName )
        fclose( m_tFile );
    m_tFile = NULL;
}

bool FileOnDisk_c::IsExists( const char * szName )
{
    FILE * tFile = fopen( szName, "r" );
    if( tFile )
    {
        fclose( tFile );
        return true;
    }
    else 
        return false;
}

int FileOnDisk_c::Write( const void* pData, int nBytes )
{
    int nWritten = (int)fwrite( pData, 1, nBytes, m_tFile );
    Assert( nWritten == nBytes && "writing error" );
    return nBytes;
}

int FileOnDisk_c::Read( void* pData, int nBytes )
{
    int nRead = (int)fread( pData, 1, nBytes, m_tFile );
    if( nRead != nBytes )
    {
        Assert( feof( m_tFile ) == 0 );
        Assert( ferror( m_tFile ) == 0 );
    }
    Assert( nRead == nBytes && "reading error" );
    return nBytes;
}

void FileOnDisk_c::Flush()
{
    fflush( m_tFile );
}

////////////////////////////////////////////////////////////////////////////

TextFile_c::TextFile_c( const char * szName, bool isRead )
:   FileOnDisk_c( szName, isRead, false )
{}

TextFile_c::~TextFile_c()
{}

bool TextFile_c::StringRead( char * pData, int nMaxLength )
{
    return fgets( pData, nMaxLength, m_tFile ) != NULL;
}

void TextFile_c::StringWrite( const char * pData )
{
    fputs( pData, m_tFile );
}

////////////////////////////////////////////////////////////////////////////

Log_c g_cLogMain    ( "main.log" );
Log_c g_cLogProgress( "progress.log" );
Log_c g_cLogStdOut  ( "", true );

Log_c::Log_c( const char *szName, bool bEcho )
:   m_cFile     ( szName, false )
,   m_nSection  ( 0 )
,   m_bEcho     ( bEcho )
{
}

Log_c::~Log_c()
{
}

void Log_c::Out( const char *szText, ... )
{
    char szBuf[MAX_LOGENTRY_LENGTH];
    if( m_nSection )
        memset( szBuf, ' ', m_nSection*LOG_SECTION_OFFSET );
    va_list tArgs;
    va_start( tArgs, szText );
    vsprintf( szBuf+m_nSection*LOG_SECTION_OFFSET, szText, tArgs );
    va_end( tArgs );
    m_cFile.StringWrite( szBuf );
    if( m_bEcho )
        g_cLogMain.m_cFile.StringWrite( szBuf );
}

void Log_c::PureOut( const char *szText, ... )
{
    char szBuf[MAX_LOGENTRY_LENGTH];
    va_list tArgs;
    va_start( tArgs, szText );
    vsprintf( szBuf, szText, tArgs );
    va_end( tArgs );
    m_cFile.StringWrite( szBuf );
    if( m_bEcho )
        g_cLogMain.m_cFile.StringWrite( szBuf );
}

void Log_c::SectionBegin()
{
    Out( "{\n" );
    ++m_nSection;
}

void Log_c::SectionEnd()
{
    --m_nSection;
    Out( "}\n" );
}

void Log_c::Flush()
{
    m_cFile.Flush();
    if( m_bEcho )
        g_cLogMain.Flush();
}

///
/// $Id: storage.cpp,v 1.4 2005/10/16 08:35:49 olegus Exp $
///

